package com.adlotto2.lcdns.cronos.http;

import java.net.URLDecoder;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;

import com.adlotto2.lcdns.util.CNCronosLog;

public class CNHttpRequest {
	public static final int HTTP_REQUEST_TIMEOUT = 15000;
	public static final String HTTP_REQUEST_CHARSET = "UTF-8";
	public static final String LOTTE_AUTH_INFO = "uinfo";
	public static final String LOTTE_REFERER = "ureferer";

	public static HttpResponse getClient(String url, String queryString) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (queryString != null) {
				requestUrl = requestUrl + "?" + queryString;
			}
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			HttpGet request = new HttpGet(requestUrl);
			request.setParams(httpParams);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	public static HttpResponse getClient(String token, String ureferer,
			String url, String queryString) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (queryString != null) {
				requestUrl = requestUrl + "?" + queryString;
			}
			CNCronosLog.d("ureferer: " + ureferer);
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			HttpGet request = new HttpGet(requestUrl);
			request.setParams(httpParams);
			request.setHeader("ureferer", ureferer);
			request.setHeader("uinfo", token);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return response;
	}

	@SuppressWarnings("unchecked")
	public static HttpResponse postClient(String url, String queryString) {
		HttpResponse response = null;
		try {
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			@SuppressWarnings("rawtypes")
			ArrayList paramList = new ArrayList();
			if ((queryString != null) && (queryString.length() > 0)) {
				String[] values1 = queryString.split("&");
				if ((values1 != null) && (values1.length > 0)) {
					for (int i = 0; i < values1.length; i++) {
						String[] values2 = values1[i].split("=");
						if ((values2 != null) && (values2.length > 1)) {
							paramList.add(new BasicNameValuePair(values2[0], URLDecoder.decode(values2[1], "UTF-8")));
						}
					}
				}
			}

			UrlEncodedFormEntity ent = new UrlEncodedFormEntity(paramList, "UTF-8");

			HttpPost request = new HttpPost(url);
			request.setParams(httpParams);
			request.setEntity(ent);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	@SuppressWarnings("unchecked")
	public static HttpResponse postClient(String token, String ureferer,
			String url, String queryString) {
		HttpResponse response = null;
		try {
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			@SuppressWarnings("rawtypes")
			ArrayList paramList = new ArrayList();
			if ((queryString != null) && (queryString.length() > 0)) {
				String[] values1 = queryString.split("&");
				if ((values1 != null) && (values1.length > 0)) {
					for (int i = 0; i < values1.length; i++) {
						String[] values2 = values1[i].split("=");
						if ((values2 != null) && (values2.length > 1)) {
							paramList.add(new BasicNameValuePair(values2[0],
									URLDecoder.decode(values2[1], "UTF-8")));
						}
					}
				}
			}

			UrlEncodedFormEntity ent = new UrlEncodedFormEntity(paramList,
					"UTF-8");

			HttpPost request = new HttpPost(url);
			request.setParams(httpParams);
			request.setHeader("ureferer", ureferer);
			request.setHeader("uinfo", token);
			request.setEntity(ent);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	public static HttpResponse deleteClient(String url, String queryString) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (queryString != null) {
				requestUrl = requestUrl + "?" + queryString;
			}
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			HttpDelete request = new HttpDelete(requestUrl);
			request.setParams(httpParams);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	public static HttpResponse deleteClient(String token, String ureferer,
			String url, String queryString) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (queryString != null) {
				requestUrl = requestUrl + "?" + queryString;
			}
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			HttpDelete request = new HttpDelete(requestUrl);
			request.setParams(httpParams);
			request.setHeader("ureferer", ureferer);
			request.setHeader("uinfo", token);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	public static HttpResponse putClient(String url, String queryString) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (queryString != null) {
				requestUrl = requestUrl + "?" + queryString;
			}
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			HttpPut request = new HttpPut(requestUrl);
			request.setParams(httpParams);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	public static HttpResponse putClient(String token, String ureferer,
			String url, String queryString) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (queryString != null) {
				requestUrl = requestUrl + "?" + queryString;
			}
			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, HTTP_REQUEST_TIMEOUT);
			HttpConnectionParams.setSoTimeout(httpParams, HTTP_REQUEST_TIMEOUT);

			HttpPut request = new HttpPut(requestUrl);
			request.setParams(httpParams);
			request.setHeader("ureferer", ureferer);
			request.setHeader("uinfo", token);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}
		return response;
	}

	/*public static HttpResponse getClientAuth(String url, String params,
			OAuthConsumer consumer) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (params != null) {
				requestUrl = requestUrl + "?" + params;
			}

			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
			HttpConnectionParams.setSoTimeout(httpParams, 15000);

			HttpGet request = new HttpGet(consumer.sign(requestUrl));
			request.setParams(httpParams);
			request.setHeader("Authorization", "OAuth");

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}

		return response;
	}

	public static HttpResponse postClientAuth(String url, String queryString,
			OAuthConsumer consumer) {
		HttpResponse response = null;
		try {
			HttpParams parms = new BasicHttpParams();
			HttpProtocolParams.setVersion(parms, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(parms, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(parms, false);
			HttpConnectionParams.setConnectionTimeout(parms, 15000);
			HttpConnectionParams.setSoTimeout(parms, 15000);

			ArrayList paramList = new ArrayList();
			if ((queryString != null) && (queryString.length() > 0)) {
				String[] valuse1 = queryString.split("&");

				if ((valuse1 != null) && (valuse1.length > 0)) {
					for (int i = 0; i < valuse1.length; i++) {
						String[] values2 = valuse1[i].split("=");

						if ((values2 != null) && (values2.length > 1)) {
							paramList.add(new BasicNameValuePair(values2[0],
									URLDecoder.decode(values2[1], "UTF-8")));
						}
					}
				}
			}

			UrlEncodedFormEntity ent = new UrlEncodedFormEntity(paramList,
					"UTF-8");

			HttpPost request = new HttpPost(url);
			request.setParams(parms);
			request.setHeader("Authorization", "OAuth");
			request.setEntity(ent);
			consumer.sign(request);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}

		return response;
	}

	public static HttpResponse deleteClientAuth(String url, String params,
			OAuthConsumer consumer) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (params != null) {
				requestUrl = requestUrl + "?" + params;
			}

			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
			HttpConnectionParams.setSoTimeout(httpParams, 15000);

			HttpDelete request = new HttpDelete(requestUrl);
			request.setParams(httpParams);
			request.setHeader("Authorization", "OAuth");
			consumer.sign(request);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}

		return response;
	}

	public static HttpResponse putClientAuth(String url, String params,
			OAuthConsumer consumer) {
		HttpResponse response = null;
		try {
			String requestUrl = url;
			if (params != null) {
				requestUrl = requestUrl + "?" + params;
			}

			HttpParams httpParams = new BasicHttpParams();
			HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
			HttpProtocolParams.setUseExpectContinue(httpParams, false);
			HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
			HttpConnectionParams.setSoTimeout(httpParams, 15000);

			HttpPut request = new HttpPut(requestUrl);
			request.setParams(httpParams);
			request.setHeader("Authorization", "OAuth");
			consumer.sign(request);

			HttpClient httpClient = new DefaultHttpClient();
			response = httpClient.execute(request);
		} catch (Exception localException) {
		}

		return response;
	}*/
}